package taskManager.dataAccessLayer;

import com.google.appengine.api.datastore.*;

public abstract class EntityBase
{
	private DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
	
	private Key entityKey;
	
	protected Key getEntityKey()
	{
		return entityKey;
	}
	
	protected Key Insert(Entity entity)
	{
		Key entityKey = datastore.put(entity);
		return entityKey;
	}
	
	protected void Update(Entity entity)
	{
		datastore.put(entity);
	}
	
	protected void Delete(Key entityKey)
	{
		datastore.delete(entityKey);
	}
	
	protected Entity GetEntityInfo(Key entityKey)
	{
		try
		{
			return datastore.get(entityKey);
		}
		catch (EntityNotFoundException e)
		{
			e.printStackTrace();
			return null;
		}
	}
	
	public Iterable<Entity> getResult(Query q)
	{
		PreparedQuery pq = datastore.prepare(q);
		return pq.asIterable();
	}
}
